home *** CD-ROM | disk | FTP | other *** search
- Path: anvil.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Generating pointer to struct from string
- Date: 27 Feb 1996 12:45:49 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4gvqhtINNi79@anvil.ugrad.cs.ubc.ca>
- References: <4grk4g$hep@fmsu03.fm.intel.com>
- NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
-
- In article <4grk4g$hep@fmsu03.fm.intel.com>,
- Vishram Dalvi <vdalvi@mcd.intel.com> wrote:
- >Hello,
- >
- >Say I have 3 structs aaa, bbb and ccc of type foo:
- >
- >struct foo {
- > int flag;
- > char name[30]
- >} aaa, bbb, ccc;
- >
- >I assign some values to the data members of all 3 structs. Now I prompt the
- >user for the name of one of these structs. I read in the user entry into a
- >char array. Assume that the user entered "aaa". How can I generate a pointer
- >to the struct "aaa" from the user entry?
- >I do not want a switch/case table (if user entered "aaa", access members of
- >"aaa", if user entered "bbb"...).
- >How do I cast a string into a struct address??
- >Thanks in advance,
-
- The names of C language objects are not accessible at run time. They are
- temporary symbols needed to achieve a translation into the execution language.
-
- This sort of thing is possible from debuggers, which have access to a symbol
- table and other debugging information emitted by the compiler. In theory, you
- could (in a very operating-system specific way) have a program search its own
- debugging information (assuming it's available) and come up with a mapping for
- a symbol name to an address.
-
- The easiest way to do it is to just declare an array of strings whose names
- match your structures, and search this array. You can sort it with qsort() and
- then use bsearch(), if it is large.
-
- Better yet, make each name a field of the structure, and put the structures
- into an array:
-
- struct foo {
- char name[8];
- int value;
- } arr[] = { { "aaa" }, { "bbb" }, { "ccc" }};
-
-
- --
-
-